    /* global hex_md5 */
    /* global getCookie */
    /* global timeToTimestamp visitUrl*/

    function submitAdd(state){
        var userMobile = $("#userMobile").val();
        var smsCode = $("#smsCode").val();
        
       
        if (userMobile == ''){
            $.toast("电话不能为空", "cancel");
            return;
        }
        if (smsCode == ''){
            $.toast("验证码不能为空", "cancel");
            return;
        }
        
        var params = {};
        params.mobile = userMobile;
        params.smsCode = smsCode;
        params.token = getCookie('token');

        add(params);
          

    } 

    function add(params){
        $.ajax({
            type:'POST',
            url:"/antique/api/sysUser/bind_mobile",
            data: params,
            dataType:"json",
            success:function(res){
                if (res.code == 0){
                    visitUrl('/my_wallet',true);
                    $.toast("绑定成功");
                 } else {
                    $.toast(res.message, "cancel");
                }
                
            }
        });
    }


    var countdown = 60; 
    function sendemail(){
        var obj = $("#sendBtn");
        var params = {};
        params.mobile = $("#userMobile").val();
        params.type = 1;

        if ($("#userMobile").val() == ''){
            $.toast("电话不能为空", "cancel");
            return;
        }
        if ($("#userMobile").val().length != 11){
            $.toast("电话格式错误", "cancel");
            return;
        }

        getSecretkey(params);
        settime(obj);
    
    }
    function settime(obj) { //发送验证码倒计时
        if (countdown == 0) { 
            obj.attr('disabled',false); 
            obj.html("获取验证码");
            countdown = 60; 
            return;
        } else { 
            obj.attr('disabled',true);
            obj.html("重新发送(" + countdown + ")");
            countdown--; 
        } 
        setTimeout(function() { settime(obj); },1000) ;
    }

    function getSecretkey(params){
        $.ajax({
            type:'POST',
            url:"/antique/api/sysSms/query_sys_secretkey",
            data: { token : getCookie('token') },
            dataType:"json",
            success:function(res){
                if (res.code == 0){
                    var secretKey = res.data.secretKey;
                    var time = timeToTimestamp(new Date());
                    params.time = time;
                    var smsSign = hex_md5(params.mobile + secretKey + secretKey + time).toUpperCase();
                    params.smsSign = smsSign;
                    sendMsm(params);
                 } else {
                    $.toast(res.message, "cancel");
                }
                
            }
        });
    }
    function sendMsm(params){
        $.ajax({
            type:'POST',
            url:"/antique/api/sysSms/send_validate_sms",
            data: params,
            dataType:"json",
            success:function(res){
                if (res.code == 0){
                    $.toast("发送成功");
                 } else {
                    $.toast(res.message, "cancel");
                }
                
            }
        });
    }


